package com.yema.mytestlibrary.mvpcontract

import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity


/**
 * <p>文件描述</p>
 * 用户名:yema
 * 时间:2019/6/25   10:40
 */
abstract class BaseActivity<V, P : BasePresenter<V>> : AppCompatActivity() {
    private var mPresenter: P? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getLayoutID())
        mPresenter = createPresenter()
        initViews(savedInstanceState)
    }

    protected abstract fun getLayoutID(): Int

    //初始化布局文件、控件
    protected abstract fun initViews(savedInstanceState: Bundle?)


    protected abstract fun createPresenter(): P

    override fun onResume() {
        super.onResume()
        mPresenter?.attachView(this as V)
        if (mPresenter!!.isViewAttached())
            loadData()
    }

    //加载数据
    abstract fun loadData()

    override fun onPause() {
        super.onPause()
        mPresenter?.clearDisposable()
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter?.detachView()

        //判断隐藏软键盘是否弹出
        if (window.attributes.softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
            //隐藏软键盘
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
        }
    }

}